<template>
  <div>
    <el-table border stripe :data="emps" size="mini">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="name" label="姓名" width="120" align="left" fixed></el-table-column>
      <el-table-column prop="workID" label="工号" width="120" align="left"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="200" align="left"></el-table-column>
      <el-table-column prop="phone" label="电话号码" width="120" align="left"></el-table-column>
      <el-table-column prop="department.name" label="部门" width="120" align="left"></el-table-column>
      <el-table-column label="工资账套" align="center">
        <template slot-scope="scope">
          <el-tooltip placement="right" v-if="scope.row.salary">
            <div slot="content">
              <table>
                <tr>
                  <td>基本工资</td>
                  <td>{{ scope.row.salary.basicSalary }}</td>
                </tr>
                <tr>
                  <td>交通补助</td>
                  <td>{{ scope.row.salary.trafficSalary }}</td>
                </tr>
                <tr>
                  <td>午餐补助</td>
                  <td>{{ scope.row.salary.lunchSalary }}</td>
                </tr>
                <tr>
                  <td>奖金</td>
                  <td>{{ scope.row.salary.bonus }}</td>
                </tr>
                <tr>
                  <td>养老金比率</td>
                  <td>{{ scope.row.salary.pensionPer }}</td>
                </tr>
                <tr>
                  <td>养老金基数</td>
                  <td>{{ scope.row.salary.pensionBase }}</td>
                </tr>
                <tr>
                  <td>医疗保险比率</td>
                  <td>{{ scope.row.salary.medicalPer }}</td>
                </tr>
                <tr>
                  <td>医疗保险基数</td>
                  <td>{{ scope.row.salary.medicalBase }}</td>
                </tr>
                <tr>
                  <td>公积金比率</td>
                  <td>{{ scope.row.salary.accumulationFundPer }}</td>
                </tr>
                <tr>
                  <td>公积金基数</td>
                  <td>{{ scope.row.salary.accumulationFundBase }}</td>
                </tr>
              </table>
            </div>
            <el-tag style="cursor: pointer" type="success">{{scope.row.salary.name}}</el-tag>
          </el-tooltip>
          <el-tag v-else style="cursor: pointer" type="danger">暂未设置</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-popover
              @show="showPop(scope.row.salary)"
              @hide="hidePop(scope.row)"
              placement="left"
              title="编辑员工工资账套"
              width="200"
              trigger="click">
<!--            下拉选择框-->
            <div>
              <el-select size="mini" v-model="currentSalary" placeholder="请选择">
                <el-option
                    v-for="item in salaries"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
                </el-option>
              </el-select>
            </div>
            <el-button type="danger" icon="el-icon-edit" slot="reference">编辑工资账套</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <!--分页按钮-->
    <div style="display: flex;justify-content: flex-end;margin-top: 5px">
      <el-pagination
          background
          @current-change="currentChange"
          @size-change="sizeChange"
          layout="sizes, prev, pager, next, jumper, ->, total, slot"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SalSobCfg',
  data() {
    return {
      currentSalary: null,
      salaries: [],
      emps: [],
      currentPage: 1,
      size: 10,
      total: 0
    }
  },
  methods: {
    hidePop(emp) {
      if (this.currentSalary && !emp.salary || (this.currentSalary && emp.salary.id !== this.currentSalary)){
        this.putRequest("/salary/sobcfg/?eid=" + emp.id + "&sid=" + this.currentSalary).then(resp => {
          if (resp) {
            this.initEmpsWithSalary();
          }
        });
      }
    },
    showPop(data) {
      if (data) {
        this.currentSalary = data.id;
      } else {
        this.currentSalary = null;
      }
    },
    initAllSalaries() {
      this.getRequest("/salary/sobcfg/salaries").then(resp => {
        if (resp) {
          this.salaries = resp;
        }
      });
    },
    sizeChange(size) {
      this.size = size;
      this.initEmpsWithSalary();
    },
    currentChange(currentPage) {
      this.currentPage = currentPage;
      this.initEmpsWithSalary();
    },
    initEmpsWithSalary() {
      this.getRequest("/salary/sobcfg/?currentPage=" + this.currentPage + "&size=" + this.size).then(resp => {
        if (resp) {
          this.emps = resp.data;
          this.total = resp.total;
        }
      });
    }
  },
  mounted() {
    this.initEmpsWithSalary();
    this.initAllSalaries();
  }
}
</script>

<style>
</style>